package com.kk.producer.comfig;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * 死信队列、交换机配置
 *    <!--
 *         死信队列：
 *             1. 声明正常的队列(test_queue_dlx)和交换机(test_exchange_dlx)
 *             2. 声明死信队列(queue_dlx)和死信交换机(exchange_dlx)
 *             3. 正常队列绑定死信交换机
 *                 设置两个参数：
 *                     * x-dead-letter-exchange：死信交换机名称
 *                     * x-dead-letter-routing-key：发送给死信交换机的routingkey
 *     -->
 */

@Configuration
public class DLXRabbitmqConfig {

    //普通交换机名称
    public static final String X_EXCHANGE="X";
    //死信交换机名称
    public static final String Y_DEAD_LETTER_EXCHANGE="Y";
    //普通队列名称
    public static final String QUEUE_A="QA";
    //死信队列名称
    public static final String DEAD_LETTER_QUEUE_D="QD";

    /**
     * 声明普通交换机 x
     * @return
     */
    @Bean("xExchange")//别名和方法名取一样
    public DirectExchange xExchange(){
        return new DirectExchange(X_EXCHANGE);
    }

    /**
     * 声明死信交换机 y
     * @return
     */
    @Bean("yExchange")//别名和方法名取一样
    public DirectExchange yExchange(){
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }

    //声明正常队列 A
    @Bean("queueA")
    public Queue queueA(){
        final HashMap<String, Object> arguments
                = new HashMap<>();

        //设置死信交换机
        arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        //设置死信RoutingKey
        arguments.put("x-dead-letter-routing-key","YD");
        //设置TTL设置10秒过期
        arguments.put("x-message-ttl",10000);
        // 设置队列长度限制
        arguments.put("x-max-length",10);

        return QueueBuilder.durable(QUEUE_A)
                .withArguments(arguments)
                .build();
    }
    //声明死信队列 D
    @Bean("queueD")
    public Queue queueD(){
        return QueueBuilder.durable(DEAD_LETTER_QUEUE_D)
                .build();
    }

    /**
     * A队列绑定X交换机
     * @param queueA
     * @return
     */
    @Bean
    public Binding queueABindingX(@Qualifier("queueA")Queue queueA,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }
    /**
     * D队列绑定死信y交换机
     * @param queueD
     * @param yExchange
     * @return
     */
    @Bean
    public Binding queueDBindingX(@Qualifier("queueD")Queue queueD,
                                  @Qualifier("yExchange") DirectExchange yExchange){
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }


}
